package com.guanyua.eims.controller;


import com.guanyua.eims.pojo.Rest;
import com.guanyua.eims.common.util.RestUtil;
import com.guanyua.eims.pojo.productcategory.Category;
import com.guanyua.eims.pojo.productcategory.CategoryAddForm;
import com.guanyua.eims.pojo.productcategory.CategoryTreeDTO;
import com.guanyua.eims.pojo.productcategory.CategoryUpdateForm;
import com.guanyua.eims.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 产品-类目 前端控制器
 * </p>
 *
 * @author 任建波
 * @since 2020-10-18
 */
@Api(tags = "类目")
@RestController
@RequestMapping("/product-category")
@Validated
@Slf4j
public class CategoryController {

    @Autowired
    CategoryService categoryService;

    @ApiOperation(value = "添加类目")
    @PostMapping(value = "addCategory")
    public Rest addCategory(@Validated CategoryAddForm form) {
        Category category = new Category();
        BeanUtils.copyProperties(form, category);

        categoryService.save(category);
        return RestUtil.success();
    }

    @ApiOperation(value = "修改类目")
    @PostMapping(value = "updateCategory")
    public Rest updateCategory(@Validated CategoryUpdateForm form) {
        Category category = new Category();
        BeanUtils.copyProperties(form, category);

        categoryService.updateById(category);
        return RestUtil.success();
    }

    @ApiOperation(value = "获取树结构类目")
    @GetMapping(value = "getCategoryTree")
    public Rest<List<CategoryTreeDTO>> getCategoryTree() {
        List<CategoryTreeDTO> categoryTree = categoryService.getCategoryTree();

        return RestUtil.success(categoryTree);
    }

}

